package com.chaowei.lims.utils;

import com.chaowei.lims.R;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

public class LoadingView {
	private Context mContext;
	private View loadingView, nothingView,errorView;
	private ListView mlist;
	private ViewGroup parent;
	private LinearLayout view;

	public LoadingView(Context context, ListView list) {
		super();
		this.mContext = context;
		this.mlist = list;
		parent = ((ViewGroup) mlist.getParent());
		view = new LinearLayout(mContext);

		addViews();

		mlist.setEmptyView(view);

	}

	private void addViews() {
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		param.gravity = Gravity.CENTER;
		view.setLayoutParams(param);

		loadingView = LayoutInflater.from(mContext).inflate(
				R.layout.view_loading, null);
		nothingView = LayoutInflater.from(mContext).inflate(
				R.layout.view_nothing, null);
		errorView = LayoutInflater.from(mContext).inflate(
				R.layout.view_error, null);
		loadingView.setLayoutParams(param);
		nothingView.setLayoutParams(param);
		errorView.setLayoutParams(param);
		loadingView.setVisibility(View.GONE);
		nothingView.setVisibility(View.GONE);
		errorView.setVisibility(View.GONE);
		view.addView(loadingView);
		view.addView(nothingView);
		view.addView(errorView);
		parent.addView(view);
	}

	public void showLoading() {
		loadingView.setVisibility(View.VISIBLE);
		nothingView.setVisibility(View.GONE);
		errorView.setVisibility(View.GONE);
	}

	public void showNothing() {
		loadingView.setVisibility(View.GONE);
		nothingView.setVisibility(View.VISIBLE);
		errorView.setVisibility(View.GONE);
	}
	
	public void showError(){
		loadingView.setVisibility(View.GONE);
		nothingView.setVisibility(View.GONE);
		errorView.setVisibility(View.VISIBLE);
	}

}
